package com.xsd.awen.utils;

import java.math.BigDecimal;

public class Numbers {

    private static final int SCALE = 6;

    /**
     * num1 + num2
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal add(BigDecimal num1,BigDecimal num2){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        return num1.add(num2).stripTrailingZeros();
    }

    /**
     * num1 - num2
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal sub(BigDecimal num1,BigDecimal num2){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        return num1.subtract(num2).stripTrailingZeros();
    }

    /**
     * num1 * num2
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal mul(BigDecimal num1,BigDecimal num2){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        return num1.multiply(num2).stripTrailingZeros();
    }

    /**
     * num1 ÷ num2 默认2位小数
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal div(BigDecimal num1,BigDecimal num2){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        return div(num1,num2, SCALE).stripTrailingZeros();
    }

    /**
     * num1 ÷ num2 scale位小数
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal div(BigDecimal num1,BigDecimal num2,int scale){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        if(scale<0){
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return num1.divide(num2,scale,BigDecimal.ROUND_HALF_UP).stripTrailingZeros();
    }

    /**
     * 取模，num1 % num2
     * @param num1
     * @param num2
     * @return
     */
    public static BigDecimal remainder(BigDecimal num1,BigDecimal num2){
        num1 = num1.stripTrailingZeros();
        num2 = num2.stripTrailingZeros();
        return num1.remainder(num2).stripTrailingZeros();
    }



}
